Functions in js
September 04, 2019
what is function
Function is the set or collection of instructions which are kept in the box called function.which when we executes it pefroms a specific task as its output..
Types of Functions In JavaScript
-
Named Function
function sum(a,b) { return a+b; }Named function means that function declaration with name eg i have given name to function above is sum
-
Anonymous Functions
var sum = function (a,b) { return a+b; }Anonymous Function or function expression means function without name
-
Immediately invoked function expression (IIFE)
function (a,b) { return a+b; })(a,b)An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
-
Function Execution Or Invocation
It simply means that the giving command to function for executing the collection of code which we have put inside it
for example: Here im executing the one of function which i have defined above
sum(1,2);*we can execute the function by its name i have provide arguments inside the brackets at time of execution so the output will be 3.the provided agruments will go inside the parameters which i define above in the function declaration.*
-
Different Components Of Functions
Genrelly The Main Components Of The Functions Are
- Function Keyword
- Function Name
- Parameters Of The Function
- Return Statement in The Function If Not Provided It Will Going To Return The Undefined
- Arguments In The Function at Execution Time